package service

import (
	"errors"
	"strconv"
	"tiktok2023/model"
	"time"

	"github.com/gin-gonic/gin"
)

type CommentService struct {
}

type CommentActionResponse struct {
	model.Response
	Comment model.Comment `json:"comment"`
}

func (comm CommentService) HandleCommentAction(c *gin.Context) (*CommentActionResponse, error) {
	var request struct {
		Token       string `json:"token" form:"token" `
		VideoID     string `json:"video_id" form:"video_id" binding:"required"`
		ActionTye   string `json:"action_type" form:"action_type" binding:"required"`
		CommentText string `json:"comment_text" form:"comment_text"`
		CommentId   string `json:"comment_id" form:"comment_id"`
	}
	if err := c.ShouldBind(&request); err != nil {
		return nil, err
	}
	userId := c.GetUint64("userId")
	// 查询用户的信息
	user := model.TableUser{}
	if err := model.Db.Where("id = ?", userId).First(&user).Error; err != nil {
		return nil, err
	}
	resp := CommentActionResponse{
		Response: model.Response{
			StatusCode: 0,
			StatusMsg:  "success",
		},
	}
	// 判断操作的类型
	if request.ActionTye == "1" {
		// 进行评论
		videoId, errToInt := strconv.Atoi(request.VideoID)
		if errToInt != nil {
			return nil, errToInt
		}
		comment := model.CommentTable{
			UserId:      userId,
			CommentText: request.CommentText,
			VideoId:     uint64(videoId),
			CreateDate:  time.Now(),
			Cancel:      1,
		}
		// 保存评论
		if err := model.Db.Create(&comment).Error; err != nil {
			return nil, err
		}
		// 查询评论者的信息
		user := model.TableUser{}
		if err := model.Db.Where("id = ?", userId).First(&user).Error; err != nil {
			return nil, err
		}
		resp.Comment.User = user
		resp.Comment.Id = comment.Id
		resp.Comment.Content = comment.CommentText
		resp.Comment.CreateDate = comment.CreateDate.Format("2006-01-02 15:04:05")
	} else if request.ActionTye == "2" {
		// 取消评论的操作
		commentId, _ := strconv.Atoi(request.CommentId)
		comment := model.Comment{}
		if err := model.Db.Model(&comment).Where("id = ?", commentId).Update("cancel = ?", 2).Error; err != nil {
			return nil, err
		}
	} else {
		return nil, errors.New("falied param")
	}
	return &resp, nil
}

type CommentListResponse struct {
	model.Response
	CommentList []model.Comment `json:"comment_list"`
}

func (comm CommentService) HandleCommentList(c *gin.Context) (resp *CommentListResponse, err error) {
	var request struct {
		VideoID string `json:"video_id" form:"video_id"` // 视频id
	}
	if err := c.ShouldBind(&request); err != nil {
		return nil, err
	}
	res := CommentListResponse{
		Response: model.Response{
			StatusCode: 0,
			StatusMsg:  "success",
		},
	}
	// 把要查的视频的评论都获取出来
	comments := []model.CommentTable{}
	videoId, err := strconv.Atoi(request.VideoID)
	if err != nil {
		return nil, err
	}
	if err := model.Db.Where("video_id = ? AND cancel = 1", videoId).Order("create_date DESC").Find(&comments).Error; err != nil {
		return nil, err
	}
	// 将用户信息也绑定上
	for _, i := range comments {
		commentTemp := model.Comment{
			Id:         i.Id,
			Content:    i.CommentText,
			CreateDate: i.CreateDate.Format("2006-01-02 15:04:05"),
		}
		user := model.TableUser{}
		if err := model.Db.Where("id = ?", i.UserId).Find(&user).Error; err != nil {
			return nil, err
		}
		commentTemp.User = user
		res.CommentList = append(res.CommentList, commentTemp)
	}
	return &res, nil
}
